package com.dream.service2.Service.Impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.dream.service2.Mapper.UserMapper;
import com.dream.service2.Service.LoginService;
import com.dream.service2.annotation.UserCheck;
import com.dream.service2.entity.User;
import com.dream.service2.utils.ReturnUtils;
import com.dream.service2.utils.ToolKits;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.*;
import java.util.List;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    UserMapper userMapper;

    @Override
    @UserCheck
    public ReturnUtils login(User user) throws RuntimeException {
        String username = user.getUsername();
        String password = SecureUtil.md5(user.getPassword());
        User u = userMapper.checkLogin(username, password);
        if (ToolKits.isEmpty(u)) throw new RuntimeException("测试异常");
        if (ToolKits.isEmpty(u)) return ReturnUtils.erro("用户名或密码错误", null);
        return ReturnUtils.success("登陆成功", null);
    }

    @Override
    public ReturnUtils register(User user) throws RuntimeException {
        String username = user.getUsername();
        String password = SecureUtil.md5(user.getPassword());//对密码进行md5加密
        User user1 = userMapper.selectByUsername(username);
        if (ToolKits.isNotEmpty(user1)) {
            return ReturnUtils.erro("该用户名已注册", null);
        }
        user.setPassword(password);
        int i = userMapper.insert(user);
        if (i > 0) {
            return ReturnUtils.success("注册成功", null);
        }
        return ReturnUtils.erro("注册失败", null);
    }

    @Override
    public List<User> selectUser() {

        return userMapper.selectList();
    }

    @Override
    public ReturnUtils test(User user) {
        register(user);

        return ReturnUtils.success("成功",userMapper.selectList());
    }
}
